﻿#include <iostream>
#include <string>
#include "YNMysql.h"
using namespace std;
using namespace YN;

int main()
{
	YNMysql mysql;
	mysql.Connect("localhost", "root", "root", "yunni");
	mysql.SetConnectTimeout(3);
	mysql.SetReconnect();
	mysql.Query("set names utf8");
	string sql = "select * from user";
	/*mysql.Query(sql.c_str());

	mysql.StoreResult();

	for (;;)
	{
		auto row = mysql.FetchRow();
		if (row.size() == 0) break;
		for (int i = 0; i < row.size(); ++i)
		{
			if (row[i].data)
				cout << row[i].data << " ";
		}
		cout << endl;
	}

	mysql.FreeResult();*/

	// XDATA data1;
	/*data1["name"] = "linLin";
	data1["size"] = "1024";
	mysql.Insert(data1, "user");*/

	/*data1["name"] = "xy_new.jpg";
	YNData data;
	data.LoadFile("xy.jpg");
	data1["data"] = data;
	data1["size"] = &data.size;
	
	mysql.InsertBin(data1, "user");

	data.Drop();*/

	//修改id=1的数据
	/*XDATA data2;
	data2["name"] = "dashuaige";
	int affected = mysql.Update(data2, "user", "where id=1");
	cout << "修改后影响的数量：" << affected << endl;*/

	//修改id为8的二进制数据
	/*XDATA data3;
	YNData fileData;
	fileData.LoadFile("xy2.jpg");
	data3["data"] = fileData;
	data3["size"] = &fileData.size;
	int affected2 = mysql.UpdateBin(data3, "user", "where id=8");
	fileData.Drop();
	cout << "修改二进制数据影响的行数是：" << affected2 << endl;*/
	/*XDATA data4;

	//测试插入utf-8
	data4["name"] = u8"我是中文";
	mysql.Insert(data4, "user");*/

	//测试插入gbk
	/*mysql.Query("set names gbk");
	{
		YNData data = "我是gbk的文字";
		string tmp = data.GBKToUTF8();
		XDATA IData;
		IData["name"] = tmp.c_str();
		mysql.Insert(IData, "t_gbk");
	}*/
	//mysql.Query("set names gbk");
	sql = "select * from user";
	mysql.Query(sql.c_str());
	mysql.StoreResult();
	for (;;)
	{
		auto row = mysql.FetchRow();
		if (row.size() == 0) break;
		// row[2].SaveFile(row[1].data);

		for (int i = 0; i < row.size(); ++i)
		{
			if (!row[i].data)
			{
				cout << "[NULL],";
				continue;
			}
			// cout << row[i].data << " ";
			switch (row[i].type)
			{
			case YN_TYPE_BLOB:
				cout << "BLOB:" << row[i].data << " ";
				break;
			case YN_TYPE_LONG:
				cout << "long:" << row[i].data << " ";
				break;
			case YN_TYPE_VARCHAR:
				cout << "varchar:" << row[i].UTF8ToGBK() << " ";
				break;
			case YN_TYPE_STRING:
				cout << "string:" << row[i].UTF8ToGBK() << " ";
				break;
			case YN_TYPE_VAR_STRING:
				cout << "varstring:" << row[i].UTF8ToGBK() << " ";
				break;
			default:
				cout << "default:" << row[i].data << " ";
			}
		}
		cout << endl;
	}
	mysql.FreeResult();

	//获取简易结果集接口测试
	/*XROWS rows = mysql.GetResult("select * from t_gbk");
	for (auto &row : rows)
	{
		if (row.size() <= 0) break;
		for (auto &ptr : row)
		{
			if (ptr.data)
			{
				cout << ptr.UTF8ToGBK() << " ";
			}
		}
		cout << endl;
	}*/

	mysql.Close();
	return 0;
}
